HBase之表空间

1、介绍
在HBase中,namespace命名空间指对一组表的逻辑分组,类似RDBMS中的database,方便对表在业务上划分。Apache HBase从0.98.0, 0.95.2两个版本开始支持namespace级别的授权操作,HBase全局管理员可以创建、修改和回收namespace的授权。

2、namespace
HBase系统默认定义了两个缺省的namespace
hbase:系统内建表,包括namespace和meta表
default:用户建表时未指定namespace的表都创建在此

创建namespace:
hbase(main):010:0* create_namespace 'datamanage'


查看namespace:
hbase(main):011:0> describe_namespace 'datamanage'
  DESCRIPTION               
   {NAME =>'datamanage'}                                                                                                                 
列出所有namespace:
hbase(main):012:0> list_namespace
	NAMESPACE
	 datamanage                                                                                                                        
	 default   
	 hbase    

在namespace下创建表:        
hbase(main):013:0> create 'datamanage:testtable','colfam01'
    => Hbase::Table - datamanage:testtable
查看namespace下的表
hbase(main):015:0> list_namespace_tables 'datamanage'
	TABLE                 
    testtable    

删除namespace:

hbase(main):016:0> drop_namespace 'datamanage'

ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace datamanage has 1 tables
        at org.apache.hadoop.hbase.master.TableNamespaceManager.remove(TableNamespaceManager.java:200)
        at org.apache.hadoop.hbase.master.HMaster.deleteNamespace(HMaster.java:2488)
        at org.apache.hadoop.hbase.master.MasterRpcServices.deleteNamespace(MasterRpcServices.java:489)
        at org.apache.hadoop.hbase.protobuf.generated.MasterProtos$MasterService$2.callBlockingMethod(MasterProtos.java:55730)
        at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:2178)
        at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:112)
        at org.apache.hadoop.hbase.ipc.RpcExecutor.consumerLoop(RpcExecutor.java:133)
        at org.apache.hadoop.hbase.ipc.RpcExecutor$1.run(RpcExecutor.java:108)
        at java.lang.Thread.run(Thread.java:745)

Here is some help for this command:
Drop the named namespace. The namespace must be empty.
#删除表
hbase(main):017:0> disable 'datamanage:testtable'
hbase(main):018:0> drop 'datamanage:testtable'
#删除datamanage命名空间
hbase(main):019:0> drop_namespace 'datamanage'

 java API:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


/**
 * Hbase namespace
 */
public class NameSpaceInHbase {
    public static void main(String[] args) throws IOException {

        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        //管理员对象
        Admin admin = connection.getAdmin();
        //创建命名空间
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("datastrom3").build();
        admin.createNamespace(namespaceDescriptor);
        //表名
        TableName tableName = TableName.valueOf("datastrom3","testtable3");
        //表描述
        HTableDescriptor desc = new HTableDescriptor(tableName);
        //列族描述datastrom
        HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfam3"));
        //表加入列族
        desc.addFamily(coldef);
        //创建表
        admin.createTable(desc);
        //校验表是否可用
        boolean avail = admin.isTableAvailable(tableName);
        System.out.println("Table available: "+avail);
    }
}

/**
 preHell:
 ========
 nextShell:
 hbase(main):005:0> list
 TABLE
 testtable2
 user
 2 row(s) in 0.0070 seconds

java output:
 =====
 Table available: true

shell:
 =====
 hbase(main):022:0> list
 TABLE
 datastrom3:testtable3
 ...
  **/

 

posted @ 2016-08-30 11:14  similarface  阅读(10664)  评论(0编辑  收藏  举报